#!/usr/bin/env ruby
# the input and output of program program eulermaker-create-image, eulermaker-create-project-ci,
# and eulermaker-create-project are more or less the same
# input:
#{
#  "data": {
#    "os_project": "guoyidong_test",
#    "owner": "yushi",
#    "lock": false,
#    "to_delete": false,
#    "users": {
#      "yushi": "maintainer"
#    },
#    "create_time": "2025-04-18T17:58:31.979+0800",
#    "emsx": "ems1",
#    "spec_branch": "openEuler-22.03-LTS-SP2",
#    "build_env_macros": "full_fetch: y\tskip_check: y\n",
#    "build_targets": [
#      {
#        "os_variant": "openEuler:22.03-LTS-SP2",
#        "flags": {
#          "build": true,
#          "publish": false
#        },
#        "ground_projects": [
#
#        ],
#        "architecture": "aarch64"
#      },
#      {
#        "os_variant": "openEuler:22.03-LTS-SP2",
#        "flags": {
#          "build": false,
#          "publish": false
#        },
#        "ground_projects": [
#
#        ],
#        "architecture": "x86_64"
#      }
#    ],
#    "package_repos": [
#      {
#        "spec_name": "bcc",
#        "spec_url": "https://gitee.com/src-openeuler/bcc.git",
#        "spec_branch": "master"
#      },
#      {
#        "spec_name": "redis",
#        "spec_url": "https://gitee.com/src-openeuler/redis.git"
#      }
#    ],
#    "package_repo_groups": [
#      {
#        "spec_group_name": "pkgbuild",
#        "spec_group_url": "https://gitee.com/xxx/pkgbuild-repos.git",
#        "spec_branch": "master"
#      }
#    ],
#    "bootstrap_rpm_repo": [
#      {
#        "name": "everything",
#        "repo": "https://eulermaker.compass-ci.openeuler.openatom.cn/api/repositories/openEuler-22.03-LTS-SP1:everything/openEuler%3A22.03-LTS-Next/"
#      }
#    ]
#  }
#}
# output:
# .result.data.os_project: guoyidong_test
# .result.data.owner: yushi
# .result.data.lock: false
# .result.data.to_delete: false
# .result.data.users.yushi: maintainer
# .result.data.create_time: 2025-04-18T17:58:31.979+0800
# .result.data.emsx: ems1
# .result.data.spec_branch: openEuler-22.03-LTS-SP2
# .result.data.build_env_macros: full_fetch: y\tskip_check: y\n
# .result.data.build_targets: ["{\"os_variant\":\"openEuler:22.03-LTS-SP2\",\"flags\":{\"build\":true,\"publish\":false},\"ground_projects\":[],\"architecture\":\"aarch64\"}", "{\"os_variant\":\"openEuler:22.03-LTS-SP2\",\"flags\":{\"build\":false,\"publish\":false},\"ground_projects\":[],\"architecture\":\"x86_64\"}"]
# .result.data.package_repos: ["{\"spec_name\":\"bcc\",\"spec_url\":\"https://gitee.com/src-openeuler/bcc.git\",\"spec_branch\":\"master\"}", "{\"spec_name\":\"redis\",\"spec_url\":\"https://gitee.com/src-openeuler/redis.git\"}"]
# .result.data.package_repo_groups: ["{\"spec_group_name\":\"pkgbuild\",\"spec_group_url\":\"https://gitee.com/xxx/pkgbuild-repos.git\",\"spec_branch\":\"master\"}"]
# .result.data.bootstrap_rpm_repo: ["{\"name\":\"everything\",\"repo\":\"https://eulermaker.compass-ci.openeuler.openatom.cn/api/repositories/openEuler-22.03-LTS-SP1:everything/openEuler%3A22.03-LTS-Next/\"}"]

require 'json'
require 'rest-client'

LKP_SRC = ENV['LKP_SRC'] || File.direname(File.dirname(File.realpath($PROGRAM_NAME)))

if ENV['RESULT_ROOT']
    fn = File.join ENV['RESULT_ROOT'], 'eulermaker_create_project_result'
elsif ARGV[0]
    fn = ARGV[0]
end

stats_json = File.read(fn)
stats = JSON::parse(stats_json)

def dfs_parse(data, prefix)
  data.each do |key, value|
    if value.is_a? Hash
      dfs_parse(value, "#{prefix}.#{key}")
    elsif value.is_a? Array
      normalized_value = Array.new
      value.each do |el|
        if el.is_a? Hash
          normalized_value << el.to_json
        else
          normalized_value << el
        end
      end
      puts "#{prefix}.#{key}: #{normalized_value}"
    elsif value.is_a? String
      puts "#{prefix}.#{key}: #{value.inspect[1..-2]}" unless value.empty?
    else
      puts "#{prefix}.#{key}: #{value}" unless value.nil?
    end
  end
end

dfs_parse(stats, ".result")
